<?php

namespace App\Kuafu\WaiMai\Model;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfWaiMaiSplit;
use App\Kuafu\WaiMai\Enum\WaiMaiCodeEnum;
use App\Kuafu\WaiMai\Exceptions\WaiMaiException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Kuafu\Base\Model\BaseModel;

class WaiMaiShopMappingModel extends BaseModel
{
    use HasFactory, KfSoftDeletes;

    protected $connection  = 'mysqlWaiMaiAll';
    protected $table       = 'waimai_shop_mappings'; //表名
    protected $primaryKey  = 'id';
    public    $timestamps  = true;
    protected $modelCached = false;

    protected $fillable = [
        'sourceType',
        'shopId',
        'thirdShopId',
        'thirdShopName',
        'appAuthToken',
    ];

    /**
     * @param int $sourceType
     * @param int $shopId
     * @param string $thirdShopId
     * @param string $thirdShopName
     * @param string $appAuthToken
     */
    public static function addData(
        int $sourceType,
        int $shopId,
        string $thirdShopId,
        string $thirdShopName,
        string $appAuthToken = ''
    ) {
        try {

            $o = self::query()->firstOrCreate([
                'sourceType'  => $sourceType,
                'shopId'      => $shopId,
            ]);

            $o->thirdShopId   = $thirdShopId;
            $o->thirdShopName = $thirdShopName;
            $o->appAuthToken  = $appAuthToken;

            return $o->save();

        } catch (\Exception $e) {
            throw new WaiMaiException(WaiMaiCodeEnum::ERR_DB_ADD);
        }
    }

    /**
     * @param int $shopId
     * @param int $sourceType
     * @return int
     * @throws WaiMaiException
     */
    public static function delData(int $shopId, int $sourceType)
    {
        try {
            return self::query()
                       ->where('shopId', $shopId)
                       ->where('sourceType', $sourceType)
                       ->update(['isDeleted' => 1]);
        } catch (\Exception $e) {
            throw new WaiMaiException(WaiMaiCodeEnum::ERR_DB_DEL);
        }
    }

    /**
     * 通过thirdShopId查询shopId
     * @param string $thirdShopId
     * @param int $sourceType
     * @return int
     */
    public static function getShopIdByThirdShopId(string $thirdShopId, int $sourceType): int
    {
        return (int)self::query()
                        ->where('thirdShopId', $thirdShopId)
                        ->where('sourceType', $sourceType)
                        ->value('shopId');
    }

    /**
     * 查询shopId某个渠道的信息
     *
     * 请使用WaiMaiShopService::getMappingCache查询!
     * @param int $shopId
     * @param int $sourceType
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function getDataByShopId(int $shopId, int $sourceType)
    {
        return WaiMaiShopMappingModel::query()
                                     ->where('shopId', $shopId)
                                     ->where('sourceType', $sourceType)
                                     ->first();
    }
}
